package hotLeetcode;

import com.wdc.queue.Arr;

import java.security.interfaces.RSAKey;
import java.util.Arrays;

public class H200Leetcode {
    public int numIslands(char[][] grid) {
        int res = 0;
        for (int r = 0; r < grid.length; r++) {
            for (int c = 0; c < grid[r].length; c++) {
                if (grid[r][c] == '1') {
                    res++;
                    dfs(grid, r, c);
                }
            }
        }



        return res;
    }

    private void dfs(char[][] grid, int r, int c) {
        if (!inArea(grid, r, c)) {
            return;
        }


        if (grid[r][c] == '0' || grid[r][c] == '2') return;

        grid[r][c] = '2';



        dfs(grid,r + 1,c);
        dfs(grid,r - 1,c);
        dfs(grid,r ,c + 1);
        dfs(grid,r ,c - 1);



    }

    private boolean inArea(char[][] grid, int r, int c) {
        return r >= 0 && r < grid.length && c >= 0 && c < grid[r].length;
    }

    public static void main(String[] args) {
        char[][] grid = {{'1', '1', '1', '1', '0'},
                        {'1', '1', '0', '1', '0'},
                        {'1', '1', '0', '0', '0'},
                        {'0', '0', '0', '0', '0'},
        };

        H200Leetcode h200Leetcode = new H200Leetcode();
        h200Leetcode.numIslands(grid);
        for (char[] chars : grid) {
            System.out.println(Arrays.toString(chars));
        }
    }


}
